package com.devops.admin.graphql;

import cn.hutool.core.util.StrUtil;
import com.devops.admin.model.DOSProjectVersionOperateAction;
import com.devops.admin.model.Stock;
import com.devops.admin.model.TableResult;
import com.devops.admin.po.DOSProjectEnvTypePo;
import com.devops.admin.po.DOSProjectVersionPhaseListPo;
import com.devops.admin.po.DOSProjectVersionPhasePo;
import com.devops.admin.service.DOSProjectEnvClusterNamespaceRelationService;
import com.devops.admin.service.DOSProjectVersionOperateActionService;
import com.devops.admin.service.DOSProjectVersionService;
import com.devops.admin.util.Assert;
import com.devops.admin.vo.DOSPageVo;
import com.devops.admin.vo.DOSVersionOperationVo;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.List;

/**
 * @ClassName DOSProjectVersionOperateDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/4 10:44
 * @Version 1.0
 **/
@DgsComponent
public class DOSProjectVersionOperateDataFetcher {

    @Autowired
    private DOSProjectVersionOperateActionService dosProjectVersionOperateActionService;
    @Autowired
    private DOSProjectVersionService dosProjectVersionService;
    @Autowired
    private DOSProjectEnvClusterNamespaceRelationService dosProjectEnvClusterNamespaceRelationService;

    /**
     * @Author heyabo
     * @Description 创建版本
     * @Date 08:51 2022/6/23
     * @Param [operation]
     * @return com.devops.admin.po.DOSProjectVersionPhaseListPo
     **/
    @DgsMutation
    @DOSLog(description = "创建版本")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public DOSProjectVersionPhaseListPo creatVersion(@InputArgument DOSVersionOperationVo operation){

        return dosProjectVersionService.creatVersion(operation);
    }

    /**
     * @Author heyabo
     * @Description 流程操作
     * @Date 08:52 2022/6/24
     * @Param [operation]
     * @return com.devops.admin.po.DOSProjectVersionPhaseListPo
     **/
    @DgsMutation
    @DOSLog(description = "流程操作")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public DOSProjectVersionPhaseListPo processOperation(@InputArgument DOSVersionOperationVo operation){

        return dosProjectVersionService.processOperation(operation);
    }

    /**
     * @Author heyabo
     * @Description 测试完成
     * @Date 08:52 2022/6/24
     * @Param [operation]
     * @return com.devops.admin.po.DOSProjectVersionPhaseListPo
     **/
    public DOSProjectVersionPhaseListPo testEnd(DOSVersionOperationVo operation){
        operation.setOperationType(DevOpsConstant.ProjectVersionOperateActionType.TEST_FINISH);
        return dosProjectVersionService.processOperation(operation);
    }

    /**
     * @Author heyabo
     * @Description 审核通过
     * @Date 10:17 2022/6/24
     * @Param [operation]
     * @return com.devops.admin.po.DOSProjectVersionPhaseListPo
     **/
    public DOSProjectVersionPhaseListPo reviewAccept(DOSVersionOperationVo operation){
        operation.setOperationType(DevOpsConstant.ProjectVersionOperateActionType.REVIEW_ACCEPT);
        return dosProjectVersionService.processOperation(operation);
    }

    /**
     * @Author heyabo
     * @Description 发布完成
     * @Date 10:51 2022/6/24
     * @Param [operation]
     * @return com.devops.admin.po.DOSProjectVersionPhaseListPo
     **/
    public DOSProjectVersionPhaseListPo publishFinish(DOSVersionOperationVo operation){
        operation.setOperationType(DevOpsConstant.ProjectVersionOperateActionType.PUBLISH_FINISH);
        return dosProjectVersionService.processOperation(operation);
    }

    /**
     * @Author heyabo
     * @Description 审核拒绝
     * @Date 11:05 2022/6/24
     * @Param []
     * @return com.devops.admin.po.DOSProjectVersionPhaseListPo
     **/
    public DOSProjectVersionPhaseListPo reviewReject(DOSVersionOperationVo operation){
        Assert.isTrue(StrUtil.isNotBlank(operation.getOperateMsg()),"拒绝信息不能为空");
        operation.setOperationType(DevOpsConstant.ProjectVersionOperateActionType.REVIEW_REJECT);
        return dosProjectVersionService.processOperation(operation);
    }

    /**
     * @Author heyabo
     * @Description 用户确认
     * @Date 11:25 2022/6/24
     * @Param [operation]
     * @return com.devops.admin.po.DOSProjectVersionPhaseListPo
     **/
    public DOSProjectVersionPhaseListPo userConfirm(DOSVersionOperationVo operation){
        operation.setOperationType(DevOpsConstant.ProjectVersionOperateActionType.USER_CONFIRM);
        return dosProjectVersionService.processOperation(operation);
    }

    /**
     * @Author heyabo
     * @Description 获取项目版本操作字典
     * @Date 09:31 2022/6/21
     * @Param []
     * @return java.util.List<com.devops.admin.model.DOSProjectVersionOperateAction>
     **/
    @DgsQuery
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSProjectVersionOperateAction> getOperateAction(){

        return dosProjectVersionOperateActionService.list();
    }

    /**
     * @Author heyabo
     * @Description 分页查询版本数据
     * @Date 17:25 2022/6/23
     * @Param [operation, pageVo]
     * @return com.devops.admin.model.TableResult<com.devops.admin.po.DOSProjectVersionPo>
     **/
    @DgsQuery
    @DOSLog(description = "分页查询版本数据")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public TableResult<DOSProjectVersionPhasePo> getVersionPage(@InputArgument DOSVersionOperationVo operation, @InputArgument DOSPageVo pageVo){

        return dosProjectVersionService.dealProjectVersionPhasePoInfo(operation,pageVo);
    }

    /**
     * @Author heyabo
     * @Description 查询待审核的项目列表
     * @Date 15:49 2022/6/24
     * @Param [operation, pageVo]
     * @return com.devops.admin.model.TableResult<com.devops.admin.po.DOSProjectVersionPo>
     **/
    @DgsQuery
    @DOSLog(description = "查询待审核的项目列表")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.QA_ENGINEER,
            DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    public TableResult<DOSProjectVersionPhasePo> getToBeReviewed(@InputArgument DOSVersionOperationVo operation, @InputArgument DOSPageVo pageVo){

        return dosProjectVersionService.getToBeReviewed(operation,pageVo);
    }

    /**
     * @Author heyabo
     * @Description 根据版本号查询版本的详细信息
     * @Date 15:31 2022/6/28
     * @Param [versionId]
     * @return com.devops.admin.po.DOSProjectVersionPhaseListPo
     **/
    @DgsQuery
    @DOSLog(description = "根据版本号查询版本的详细信息")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public DOSProjectVersionPhaseListPo getVersionInfo(@InputArgument DOSVersionOperationVo operation){

        Assert.isTrue(operation.getVersionId() != null && operation.getVersionId() > 0,"版本号id不能为空");
        return dosProjectVersionService.dealProjectVersionPhaseListPoInfo(operation);
    }

    /**
     * @Author heyabo
     * @Description 获取可以选择的环境类型
     * @Date 14:59 2022/6/30
     * @Param [projectId]
     * @return com.devops.admin.po.DOSProjectEnvTypePo
     **/
    @DgsQuery
    public List<DOSProjectEnvTypePo> getSelectEnvType(@InputArgument Integer projectId){

        return dosProjectEnvClusterNamespaceRelationService.getSelectEnvType(projectId);
    }

    /**
     * 监听版本的状态
     * @param versionId
     * @return
     */
    @DgsSubscription
//    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public Publisher<Integer> monitorVersionStatus(@InputArgument Integer versionId ,String num){

        return dosProjectVersionService.monitorVersionStatus(versionId,"review");
    }

    @DgsSubscription
    public Publisher<Integer> monitorVersionEnd(@InputArgument Integer versionId ,String num){

        return dosProjectVersionService.monitorVersionStatus(versionId,"end");
    }
}
